<template>
  <div class="container-page container-page-list">
    <el-pagination
      class="item-pagination"
      background
      :layout="paginationLayout"
      :current-page="currentPage"
      :page-size="pageSize"
      :page-sizes="pageSizes"
      :total="total"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
    />
    <el-table border :data="list" size="mini" :lazy="true">
      <el-table-column label="更多" type="expand" width="50">
        <template slot-scope="props" label-position="right" label-width="80px" size="mini">
          <el-form>
            <el-form-item v-if="props.row.tag" label="标签">
              <itemTags :value="props.row.tag" />
            </el-form-item>
            <el-form-item v-if="props.row.procucer" :label="props.row.type==5?'关键词':'出品'">
              <itemTags :value="props.row.procucer" />
            </el-form-item>
            <el-form-item label="封面">
              <el-image :src="props.row.cover" fit="cover" />
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title" header-align="center" />
      <el-table-column label="简介" header-align="center">
        <template slot-scope="props">
          <div :title="props.row.description" v-html="props.row.description" /></template>
      </el-table-column>
      <el-table-column label="点击量" prop="hit" width="60" align="right" header-align="center" />
      <el-table-column label="添加时间" width="150" align="center">
        <template slot-scope="props">
          <span class="el-icon-date"> {{ new Date(props.row.created_at) | parseTime('{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="props">
          <el-button size="mini" plain class="el-icon-edit" type="primary" @click="updateProject(props.row.id)" />
          <el-button size="mini" plain class="el-icon-delete" type="danger" @click="deleteProject(props.row.id)" />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="item-pagination"
      background
      :layout="paginationLayout"
      :current-page="currentPage"
      :page-size="pageSize"
      :page-sizes="pageSizes"
      :total="total"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
// api
import { getProjectList, deleteProject } from '@/api/mivawork/project.js';

export default {
  data() {
    return {
      list: [],
      total: 0,
      pageSize: 10,
      pageSizes: [5, 10, 15],
      currentPage: 1,
      paginationLayout: 'prev, pager, next,sizes'
    };
  },
  async mounted() {
    await this.getProjectList();
  },
  methods: {
    async getProjectList() {
      const res = await getProjectList({
        project_type: this.nowType,
        page: this.currentPage,
        size: this.pageSize
      });
      console.log(res);
      this.list = res.data.rows;
      this.total = res.data.count;
    },
    // 删除项目
    async deleteProject(id) {
      this.$confirm('确认删除嘛?', {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      })
        .then(async() => {
          await deleteProject(id);
          await this.getProjectList();
        })
        .catch(() => {});
    },
    // 切换页面
    async handlePageChange(nowPage) {
      this.currentPage = nowPage;
      await this.getProjectList();
    },
    async handleSizeChange(nowSize) {
      this.pageSize = nowSize;
      await this.getProjectList();
    }
  }
};
</script>

<style lang="scss" scoped>
.container-page-list {
  .item {
    &-pagination {
      margin: 30px 0;
    }
    &-cover{
      // height: 40px;
    }
  }
}
</style>
